from sanic.views import HTTPMethodView
from sanic.response import json
from processing.other_func import ProToken,Sercret_all,Serialization,Random_id,date_format
import json as jn
from processing.Sql_DataFrom import ProJosn


class themeAdd(HTTPMethodView):
    async def post(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip
        html_data = request.json

        # 查询商家有没有主题
        sql_find = """
                select id from theme  where merchant_id=%s and plugin_id=%s
            """

        data = db.find_all(sql_find, (user_id, html_data['plugin_id']))

        try:
            if data:
                sql = """
                         update theme set main_color=%s,assist_color=%s,main_font_color=%s,font_assist=%s,tabbar=%s,type=%s,status=%s,ctime=now() where plugin_id=%s and merchant_id=%s
                    """

                db.update(sql, (html_data['main_color'], html_data['assist_color'], html_data['main_font_color'],
                                html_data['font_assist'], html_data['tabbar'], html_data['type'], html_data['status'],
                                html_data['plugin_id'], user_id
                                ))
                return json({'code': 200, 'message': "编辑成功!"})
            else:
                sql = """
                        insert into theme(merchant_id,plugin_id,main_color,assist_color,main_font_color,font_assist,tabbar,type,status,ctime) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,now())
                    """
                db.insert(sql, (user_id, html_data['plugin_id'], html_data['main_color'], html_data['assist_color'],
                                html_data['main_font_color'], html_data['font_assist'], html_data['tabbar'],
                                html_data['type'], html_data['status']
                                ))
                return json({'code': 200, 'message': "添加成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/theme/update', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)
class themeList(HTTPMethodView):
    async def get(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip
        plugin_id = request.args.get('plugin_id') or ''
        try:
            # 查询商家有没有主题
            sql = """
                       select id,main_color,assist_color,main_font_color,font_assist,tabbar from theme  where merchant_id=%s and plugin_id=%s
                   """

            data = db.find_all(sql, (user_id, plugin_id))
            if data:
                data = ProJosn(data, sql).to_dict(orient='records')
            else:
                data = []
            return json({'code': 200, 'message': "请求成功!", 'data': data})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/theme/list', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)

class main_activeList(HTTPMethodView):
    async def get(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip

        plugin_id = request.args.get('plugin_id') or ''
        id = request.args.get('id') or ''
        try:
            # 查询商家有没有主题
            sql = """
                       select id,title,type,content,image,details,preview,apply,profit,stimulateAD,transpond,Text,label,recommend,status from main_active  where merchant_id=%s and plugin_id=%s
                   """
            if id:
                sql += f" and id={id} "

            sql += " ORDER BY ctime DESC "
            data = db.find_all(sql, (user_id, plugin_id))
            if data:
                data = ProJosn(data, sql).to_dict(orient='records')
            else:
                data = []
            return json({'code': 200, 'message': "请求成功!", 'data': data})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/main_active/list', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)

class main_activeAdd(HTTPMethodView):
    async def post(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip
        html_data = request.json

        try:

            sql = """
                      insert into main_active(merchant_id,plugin_id,title,content,image,details,type,preview,apply,profit,stimulateAD,
                      transpond,Text,label,recommend,status,ctime ) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,now())
                  """
            db.insert(sql, (
            user_id, html_data['plugin_id'], html_data['title'], html_data['content'], html_data['image'],
            html_data['details'], html_data['type']
            , html_data['preview'] or 0, html_data['apply'] or 0, html_data['profit'] or 0,
            html_data['stimulateAD'] or 0, html_data['transpond'] or 0
            , html_data['Text'], html_data['label'], html_data['recommend'], html_data['status']))

            return json({'code': 200, 'message': "添加成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/main_active/add', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)
class main_activeDelete(HTTPMethodView):
    async def post(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip
        html_data = request.json

        try:
            sql = "delete from main_active where id=%s"
            db.delete(sql, (html_data['id']))
            return json({'code': 200, 'message': "删除成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/main_active/delete', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)
class main_activeUpdate(HTTPMethodView):
    async def post(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip

        html_data = request.json

        try:

            sql = """
                            update main_active set title=%s,content=%s,image=%s,details=%s,type=%s,Text=%s,label=%s,recommend=%s,status=%s,ctime=now() where id=%s
                        """
            db.update(sql, (
                html_data['title'],
                html_data['content'],
                html_data['image'],
                html_data['details'],
                html_data['type'],
                html_data['Text'],
                html_data['label'],
                html_data['recommend'],
                html_data['status'],
                html_data['id'],  # 不能为空
            ))

            return json({'code': 200, 'message': "编辑成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/main_active/update', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)

class bannerAdd(HTTPMethodView):
    async def post(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip
        html_data = request.json

        banner_id = Random_id(32, "BANNER")
        try:
            # 查询商家有没有主题
            sql = """
                       insert into banner(merchant_id,plugin_id,banner_id,title,target_data,target_url,type,subhead,image_list,
                       status,ctime ) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,now())
                   """
            db.insert(sql, (user_id, html_data['plugin_id'], banner_id, html_data['title'], html_data['target_data'],
                            html_data['target_url'], html_data['type']
                            , html_data['subhead'], html_data['image_list'], html_data['status']))

            return json({'code': 200, 'message': "添加成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/banner/add', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)
class bannerList(HTTPMethodView):
    async def get(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip

        plugin_id = request.args.get('plugin_id') or ''
        id = request.args.get('id') or ''
        print(321321, plugin_id)
        try:

            sql = """
                          select id,banner_id,title,target_data,target_url,type,subhead,image_list,status from banner  where merchant_id=%s and plugin_id=%s
                      """

            if id:
                sql += f" and id={id} "

            sql += " ORDER BY ctime DESC "
            data = db.find_all(sql, (user_id, plugin_id))
            if data:
                data = ProJosn(data, sql).to_dict(orient='records')
            else:
                data = []

            return json({'code': 200,
                         'data': data,
                         'message': "请求成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/banner/list', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)
class bannerDelete(HTTPMethodView):
    async def post(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip
        html_data = request.json

        try:
            sql = "delete from banner where id=%s"
            db.delete(sql, (html_data['id']))
            return json({'code': 200, 'message': "删除成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/banner/delete', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)
class bannerUpdate(HTTPMethodView):
    async def post(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip

        html_data = request.json

        try:

            sql = """
                            update banner set title=%s,target_data=%s,target_url=%s,type=%s,subhead=%s,image_list=%s,status=%s,ctime=now() where id=%s
                        """
            db.update(sql, (
                html_data['title'],
                html_data['target_data'],
                html_data['target_url'],
                html_data['type'],
                html_data['subhead'],
                html_data['image_list'],
                html_data['status'],
                html_data['id'],  # 不能为空
            ))

            return json({'code': 200, 'message': "编辑成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/banner/update', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)
class noticeAdd(HTTPMethodView):
    async def post(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip
        html_data = request.json

        try:

            sql = """
                       insert into notice(merchant_id,plugin_id,title,sort,startTime,endTime,image,type,content,
                       status,ctime ) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,now())
                   """
            db.insert(sql, (
            user_id, html_data['plugin_id'], html_data['title'], html_data['sort'] or 0, html_data['startTime'],
            html_data['endTime']
            , html_data['image'], html_data['type'], html_data['content'], html_data['status']))

            return json({'code': 200, 'message': "添加成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/notice/add', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)
class noticeList(HTTPMethodView):
    async def get(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip
        plugin_id = request.args.get('plugin_id') or ''
        id = request.args.get('id') or ''
        try:

            sql = """
                          select id,content,title,sort,startTime,endTime,image,type,status from notice  where merchant_id=%s and plugin_id=%s
                      """
            if id:
                sql += f" and id={id} "

            sql += " ORDER BY ctime DESC "
            data = db.find_all(sql, (user_id, plugin_id))
            if data:
                data = ProJosn(data, sql).to_dict(orient='records')
            else:
                data = []

            return json({'code': 200,
                         'data': data,
                         'message': "请求成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/notice/list', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)
class noticeDelete(HTTPMethodView):
    async def post(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip
        html_data = request.json

        try:
            sql = "delete from notice where id=%s"
            db.delete(sql, (html_data['id']))
            return json({'code': 200, 'message': "删除成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/notice/delete', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)
class noticeUpdate(HTTPMethodView):
    async def post(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip

        html_data = request.json

        try:

            sql = """
                            update notice set title=%s,content=%s,sort=%s,startTime=%s,endTime=%s,image=%s,type=%s,status=%s,ctime=now() where id=%s
                        """
            db.update(sql, (
                html_data['title'],
                html_data['content'],
                html_data['sort'],
                html_data['startTime'],
                html_data['endTime'],
                html_data['image'],
                html_data['type'],
                html_data['status'],
                html_data['id'],  # 不能为空
            ))

            return json({'code': 200, 'message': "编辑成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/notice/update', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)
class rechargeshow(HTTPMethodView):
    async def get(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip
        plugin_id = request.args.get('plugin_id') or ''
        print(666, plugin_id)
        id = request.args.get('id') or ''
        try:

            sql = """
                          select id,currency_balance,recharge_type_id,payment_cash,give_cash,recharge_type,channel,plugin_id,status,JSONData,create_time from rechargeshow  where merchant_id=%s and plugin_id=%s
                      """
            if id:
                sql += f" and id={id} "

            sql += " ORDER BY create_time DESC "
            data = db.find_all(sql, (user_id, plugin_id))
            if data:
                data = ProJosn(data, sql).to_dict(orient='records')
            else:
                data = []

            return json({'code': 200,
                         'data': data,
                         'message': "请求成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/rechargeshow/list', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)
class rechargeshowAdd(HTTPMethodView):
    async def post(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip
        html_data = request.json
        recharge_type_id = Random_id(32, "RECHARGE")
        try:

            sql = """
                       insert into rechargeshow(merchant_id,plugin_id,recharge_type_id,currency_balance,payment_cash,give_cash,recharge_type,JSONData,channel,
                       status,create_time ) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,now())
                   """
            db.insert(sql, (user_id, html_data['plugin_id'], recharge_type_id, int(html_data['currency_balance']),
                            int(html_data['payment_cash']), int(html_data['give_cash']) or 0,
                            html_data['recharge_type'] or '商家充值', html_data['JSONData'] or '',
                            html_data['channel'] or '', html_data['status']))

            return json({'code': 200, 'message': "添加成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/rechargeshow/add', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)

class rechargeshowDelete(HTTPMethodView):
    async def post(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip
        html_data = request.json

        try:
            sql = "delete from rechargeshow where id=%s"
            db.delete(sql, (html_data['id']))
            return json({'code': 200, 'message': "删除成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/rechargeshow/delete', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)
class rechargeshowUpdate(HTTPMethodView):
    async def post(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip

        html_data = request.json

        try:

            sql = """
                            update rechargeshow set currency_balance=%s,payment_cash=%s,give_cash=%s,recharge_type=%s,channel=%s,JSONData=%s,status=%s,create_time=now() where id=%s
                        """
            db.update(sql, (
                html_data['currency_balance'],
                html_data['payment_cash'],
                html_data['give_cash'],
                html_data['recharge_type'],
                html_data['channel'],
                html_data['JSONData'],
                html_data['status'],
                html_data['id'],  # 不能为空
            ))

            return json({'code': 200, 'message': "编辑成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/notice/update', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)
class find_user(HTTPMethodView):
    async def get(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip
        keyword = request.args.get('keyword') or ''
        try:
            sql = f"""
                          select id,openid,nickName,age,gender,head_img,phone from c_users  
                    where merchant_id='{user_id}' and (nickName like '%{keyword}%' or openid like '%{keyword}%' or phone like '%{keyword}%')
                      """
            # sql += f"""  and nickName like "%123%" """

            data = db.find_all(sql)
            if data:
                data = ProJosn(data, sql).to_dict(orient='records')
            else:
                data = []

            return json({'code': 200,
                         'data': data,
                         'message': "请求成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/rechargeshow/list', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)
# 充值成功
class rechargerecord(HTTPMethodView):
    async def post(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip
        html_data = request.json
        try:
            sql = """
                       insert into rechargerecord(merchant_id,recharge_id,merchant_number,phone,payment_number,recharge_balance,payment_cash,
                       payment_type,channel,residue,status,create_time ) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,now())
                   """
            db.insert(sql, (user_id,
                            html_data['recharge_type_id'],
                            html_data['merchant_number'],
                            html_data['phone'],
                            html_data['payment_number'],  # 支付单号
                            int(html_data['recharge_balance']) or 0,
                            int(html_data['payment_cash']) or 0,
                            html_data['payment_type'] or '',
                            html_data['channel'] or '',
                            float(html_data['recharge_balance']) or 0,
                            html_data['status']))  # 1.支付成功,2.支付失败,3.未支付

            return json({'code': 200, 'message': "充值成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/rechargeshow/add', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)


# 添加会员格式
class memberChargeAdd(HTTPMethodView):
    async def post(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip
        html_data = request.json
        memberId = Random_id(32,'MEMBER')
        try:
            sql = """
                       insert into user_level(merchant_id,plugin_id,level_id,title,type,level_score,level_order,
                       level_money,level_experience,level_payment,level_payday,level_logo,discount,status,ctime ) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,now())
                   """
            db.insert(sql, (
                    user_id,
                    html_data['plugin_id'],
                    memberId,
                    html_data['title'],  #
                    html_data['type'] or '',
                    html_data['level_score'] or '',
                    html_data['level_order'] or '',
                    html_data['level_money'] or '',
                    html_data['level_experience'] or '',
                    html_data['level_payment'] or '',
                    html_data['level_payday'] or '',
                    html_data['level_logo'] or '',
                    html_data['discount'] or '',
                    html_data['status']) or '1'
            )
            return json({'code': 200, 'message': "插入成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/memberCharge/add', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)

# 会员列表
class memberChargeList(HTTPMethodView):
    async def get(self, request):
        db = request.app.ctx.db
        user_id = request.app.ctx.user_id
        client_ip = request.ip
        pageNum = int(request.args.get('pageNum'))
        pageSize = int(request.args.get('pageSize'))
        try:

            sql = """
                select level_id,title,type,level_score,level_order,level_money,level_experience,level_payment,level_payday,level_logo,discount from user_level where merchant_id=%s
            """

            sql += f' ORDER BY ctime DESC '
            # 分页pageNum
            if pageNum == 1:
                sql += f" LIMIT {pageSize} "
            else:
                sql += f" LIMIT {pageNum * pageSize - pageSize},{pageNum * pageSize} "

            data = db.find_all(sql,(user_id))
            data = ProJosn(data,sql).to_dict(orient='records')
            return json({'code': 200, 'message': "请求成功!",'data':data})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/memberCharge/list', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)

# 删除会员格式
class memberChargeDel(HTTPMethodView):
    async def post(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip
        html_data = request.json

        try:
            sql = "delete from user_level where merchant_id=%s and level_id=%s"

            db.delete(sql, (user_id,html_data['level_id']))

            sql1 = "select * from user_level where  level_id=%s"
            if db.find_one(sql1, html_data['level_id']):
                data = {
                    'code': 400,
                    'message': '删除失败!'
                }
            else:
                data = {
                    'code': 200,
                    'message': '删除成功!'
                }

            return json(data)


        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/memberCharge/delete', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)


# 添加会员格式
class memberChargeUpdate(HTTPMethodView):
    async def post(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip
        html_data = request.json

        try:
            sql = """
                   update user_level set title=%s,type=%s,level_score=%s,level_order=%s,level_money=%s,level_experience=%s,level_payment=%s,level_payday=%s,
                   level_logo=%s,discount=%s,status=%s,ctime=now() where level_id=%s; 
               """

            db.update(sql,(
                html_data['title'],
                html_data['type'],
                html_data['level_score']or '',
                html_data['level_order']or '',
                html_data['level_money']or '',
                html_data['level_experience']or '',
                html_data['level_payment']or '',
                html_data['level_payday']or '',
                html_data['level_logo']or '',
                html_data['discount']or '',
                html_data['status'] or '1',
                html_data['level_id']
            ))

            return json({'code': 200, 'message': "修改成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/memberCharge/update', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)